package com.wzh.activity.service.impl;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.activity.dao.ActivityDao;
import com.wzh.activity.entity.ActivityEntity;
import com.wzh.activity.service.ActivityService;


@Service("activityService")
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, ActivityEntity> implements ActivityService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object o = redisTemplate.boundHashOps("activity").get("list");
        if (o==null){
            RLock lock3 = redissonClient.getLock("lock3");
            try {
                boolean b = lock3.tryLock();
                if (b){
                    System.out.println("查数据库");
                    IPage<ActivityEntity> page = this.page(
                            new Query<ActivityEntity>().getPage(params),
                            new QueryWrapper<ActivityEntity>()
                    );
                    redisTemplate.boundHashOps("activity").put("list",page);
                    return new PageUtils(page);
                }else {
                    queryPage(params);
                }
            } finally {
                lock3.unlock();
            }
        }else {
            System.out.println("缓存命中");
            IPage<ActivityEntity> page= (IPage<ActivityEntity>) redisTemplate.boundHashOps("activity").get("list");
           return new PageUtils(page);
        }
        return null;
    }

}